#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //this->setAttribute(Qt::WA_Hover, true);
    ui->labHover->installEventFilter(this);
    ui->labDBClick->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->labHover)
    {
        if(event->type() == QEvent::Enter)
            ui->labHover->setStyleSheet("background-color:rgb(170, 255, 255)");
        else if(event->type() == QEvent::Leave)
        {
            ui->labHover->setStyleSheet("");
            ui->labHover->setText("靠近我，点击我");
        }
        else if(event->type() == QEvent::MouseButtonPress)
            ui->labHover->setText("button pressed");
        else if(event->type() == QEvent::MouseButtonRelease)
            ui->labHover->setText("button released");
    }

    if(watched == ui->labDBClick)
    {
        if(event->type() == QEvent::Enter)
            ui->labDBClick->setStyleSheet("background-color: rgb(85, 255, 127)");
        else if(event->type() == QEvent::Leave)
        {
            ui->labDBClick->setStyleSheet("");
            ui->labDBClick->setText("可双击的标签");
        }
        else if(event->type() == QEvent::MouseButtonDblClick)
            ui->labDBClick->setText("double clicked");
    }

    return QWidget::eventFilter(watched, event);
}
